package DataBases;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

public class DBDirectWater {

	private static String path = ".\\src\\DataBases\\DirectWater.txt";
	private static DBDirectWater instance = null;
	private Vector<DirectWaterObject> dB;


	private DBDirectWater(){
		dB = new Vector<DirectWaterObject>();
		init();
	}
	private void init(){
		DirectWaterObject item;
		StringTokenizer token;
		String o, d;
		int t;
		boolean bypass;
		BufferedReader reader;

		try {
			reader = new BufferedReader(new FileReader(new File(path)));
			String line = null;
			while((line = reader.readLine()) != null){
				token = new StringTokenizer(line,";");
				o = token.nextToken();
				d = token.nextToken();
				t = new Integer(token.nextToken());
				bypass = new Boolean(token.nextToken());
				item = new DirectWaterObject(o, d, t, bypass);
				item.print();
				dB.add(item);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static DBDirectWater getInstance(){
		if(instance==null){
			instance = new DBDirectWater();
		}
		return instance;
	}

	public List<DirectWaterObject> getAllIncomeConnections(String dest){

		List<DirectWaterObject> result = new ArrayList<DirectWaterObject>();
		DirectWaterObject aux;
		Iterator<DirectWaterObject> itr = dB.iterator();
		while(itr.hasNext()){
			aux = itr.next();
			if( dest.compareTo(aux.getDestino()) == 0){
				result.add(aux);
			}
		}
		return result;
	}

	public Vector<DirectWaterObject> getAllOutcomeConnections(String orig){

		Vector<DirectWaterObject> result = new Vector<DirectWaterObject>();
		DirectWaterObject aux;
		Iterator<DirectWaterObject> itr = dB.iterator();
		while(itr.hasNext()){
			aux = itr.next();
			if( orig.compareTo(aux.getOrigen()) == 0){
				result.add(aux);
			}
		}
		return result;
	}

	public String getMyDestination(String orig){
		DirectWaterObject aux;
		String result = "";
		Iterator<DirectWaterObject> itr = dB.iterator();
		while(itr.hasNext()){
			aux = itr.next();
			if( orig.compareTo(aux.getOrigen()) == 0){
				result = aux.getDestino();
			}
		}
		return result;
	}

	public String getMyCloserTank(String wwtp){
		
		//Retorna el tank conectado a la entrada de la wwtp
		DirectWaterObject aux;
		Iterator<DirectWaterObject> itr = dB.iterator();
		while(itr.hasNext()){
			aux = itr.next();
			if( wwtp.compareTo(aux.getDestino()) == 0 && aux.getOrigen().contains("Tank") && !aux.isBypass()){
				return aux.getOrigen();
			}
		}
		return null;
		
		//pueden haber tank mas cercano pero no directamente conectado
		/*
		DirectWaterObject aux;
		String result = "";
		Iterator<DirectWaterObject> itr = dB.iterator();
		long tankTime = Long.MAX_VALUE;
		while(itr.hasNext()){
			aux = itr.next();
			if( orig.compareTo(aux.getOrigen()) == 0 && aux.getDestino().contains("Tank")){
				if(aux.getTiempoDeTransito() < tankTime){
					result = aux.getDestino();
				}
			}
		}
		return result;
		*/
	}

	public DirectWaterObject exists (String orig, String dest){
		Iterator<DirectWaterObject> itr = dB.iterator();
		DirectWaterObject aux;
		while(itr.hasNext()){
			aux = itr.next();
			if( orig.compareTo(aux.getOrigen()) == 0 && dest.compareTo(aux.getDestino())==0){
				return aux;
			}
		}
		return null;
	}

	public String getLaminateWWTP(String tank)
	{
		//Retorna la wwtp conectada a la salida del tank
		DirectWaterObject aux;
		Iterator<DirectWaterObject> itr = dB.iterator();
		while(itr.hasNext()){
			aux = itr.next();
			if( tank.compareTo(aux.getOrigen()) == 0 && aux.getDestino().contains("WWTP") && !aux.isBypass()){
				return aux.getDestino();
			}
		}
		return null;
	}

	public String getBypassTank(String tank)
	{
		//Retorna el tank conectado a la salida de bypass del tank param
		DirectWaterObject aux;

		Iterator<DirectWaterObject> itr = dB.iterator();
		while(itr.hasNext()){
			aux = itr.next();
			if( tank.compareTo(aux.getOrigen()) == 0 && aux.getDestino().contains("Tank") && aux.isBypass()){
				return aux.getDestino();
			}
		}
		return null;
	}

	public String getDischargeTank(String sewer)
	{
		//Retorna el tank conectado a la salida de bypass del tank param
		DirectWaterObject aux;
		
		Iterator<DirectWaterObject> itr = dB.iterator();
		while(itr.hasNext()){
			aux = itr.next();
			if( sewer.compareTo(aux.getOrigen()) == 0 && aux.getDestino().contains("Tank")&& !aux.isBypass()){
				return aux.getDestino();
			}
		}
		return null;
	}

	public long getTime(String origen, String destino) {
		DirectWaterObject aux;
		long result = 0;
		Iterator<DirectWaterObject> itr = dB.iterator();
		while(itr.hasNext()){
			aux = itr.next();
			if((origen.compareTo(aux.getOrigen()) == 0 && destino.compareTo(aux.getDestino()) == 0)
					|| (origen.compareTo(aux.getDestino()) == 0 && destino.compareTo(aux.getOrigen()) == 0)){
				result = aux.getTiempoDeTransito();
			}
		}
		return result;
	}


}
